/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.uhk.ppro.response.web;


import cz.uhk.ppro.response.model.MobileLogin;
import cz.uhk.ppro.response.model.Student;
import cz.uhk.ppro.response.service.DatabaseService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Martin
 */

@Controller
public class MobileLoginController {
    
    
    @Autowired
    private DatabaseService databaseSession;
    
    @RequestMapping(value="loginmobileresponse", method = RequestMethod.POST)
    public  @ResponseBody MobileLogin getMobileLoginInJSON(   
                                     @RequestParam(value="user",required=false) String user, 
                                     @RequestParam(value="password",required=false) String password)
      

    {
       MobileLogin login = new MobileLogin();
   
       List <Student> studenti = databaseSession.getStudentByUserAndPassword(user,password); // Sha512DigestUtils.shaHex(password)

        if(!studenti.isEmpty()){

              login.setSucces(1);
              login.setFirstName(studenti.get(0).getFirstName());
              login.setLastName(studenti.get(0).getLastName());
              login.setId(studenti.get(0).getId());
              

           }else{
              login.setSucces(0);
          }

        return login;
    }
 
}
